Skip to content

Conversation

@okaneco
Copy link
Contributor

@okaneco okaneco commented Nov 19, 2025

Feature gate: #![feature(uint_gather_scatter_bits)]
Tracking issue: #149069
Accepted ACP: rust-lang/libs-team#695 (comment)

Implement gather_bits, scatter_bits functions on unsigned integers
Add tests to coretests

This implementation is a small improvement over the plain naive form (see the solution sketch).
We only check the set bits in the mask instead of iterating over every bit.

Implement `gather_bits`, `scatter_bits` functions on unsigned integers
Add tests to coretests
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 19, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines +130 to +178
fn test_gather_bits() {
assert_eq_const_safe!($T: A.gather_bits(B), 0b_0010);
assert_eq_const_safe!($T: A.gather_bits(C), 0b_1010);

assert_eq_const_safe!($T: B.gather_bits(A), 0b_0100);
assert_eq_const_safe!($T: B.gather_bits(C), 0b_1001);

assert_eq_const_safe!($T: C.gather_bits(A), 0b_0110);
assert_eq_const_safe!($T: C.gather_bits(B), 0b_0011);

assert_eq_const_safe!($T: A.gather_bits(_0), 0);
assert_eq_const_safe!($T: B.gather_bits(_0), 0);
assert_eq_const_safe!($T: C.gather_bits(_0), 0);
assert_eq_const_safe!($T: _0.gather_bits(A), 0);
assert_eq_const_safe!($T: _0.gather_bits(B), 0);
assert_eq_const_safe!($T: _0.gather_bits(C), 0);

assert_eq_const_safe!($T: A.gather_bits(_1), A);
assert_eq_const_safe!($T: B.gather_bits(_1), B);
assert_eq_const_safe!($T: C.gather_bits(_1), C);
assert_eq_const_safe!($T: _1.gather_bits(A), 0b0000_0111);
assert_eq_const_safe!($T: _1.gather_bits(B), 0b0000_0011);
assert_eq_const_safe!($T: _1.gather_bits(C), 0b0001_1111);
}

fn test_scatter_bits() {
assert_eq_const_safe!($T: A.scatter_bits(B), 0);
assert_eq_const_safe!($T: A.scatter_bits(C), 0b0011_0000);

assert_eq_const_safe!($T: B.scatter_bits(A), 0b0000_0100);
assert_eq_const_safe!($T: B.scatter_bits(C), 0b0000_0001);

assert_eq_const_safe!($T: C.scatter_bits(A), 0b_0000_0100);
assert_eq_const_safe!($T: C.scatter_bits(B), 0b_0000_0001);

assert_eq_const_safe!($T: A.scatter_bits(_0), 0);
assert_eq_const_safe!($T: B.scatter_bits(_0), 0);
assert_eq_const_safe!($T: C.scatter_bits(_0), 0);
assert_eq_const_safe!($T: _0.scatter_bits(A), 0);
assert_eq_const_safe!($T: _0.scatter_bits(B), 0);
assert_eq_const_safe!($T: _0.scatter_bits(C), 0);

assert_eq_const_safe!($T: A.scatter_bits(_1), A);
assert_eq_const_safe!($T: B.scatter_bits(_1), B);
assert_eq_const_safe!($T: C.scatter_bits(_1), C);
assert_eq_const_safe!($T: _1.scatter_bits(A), A);
assert_eq_const_safe!($T: _1.scatter_bits(B), B);
assert_eq_const_safe!($T: _1.scatter_bits(C), C);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are difficult to manually review without seeing the constants, so I've duplicated the values and used the x86 intrinsics to show the tests produce the same values.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=72783f10e68755436d60a23a0f4a7ba4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants